//
//  CustomActionSheetAlertView.m
//  HTF_New
//
//  Created by lijinyang on 2022/8/17.
//  Copyright © 2022 htffund. All rights reserved.
//

#import "CustomActionSheetAlertView.h"

@interface CustomActionSheetAlertView ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic ,strong)UITableView *actionTabview;

@property (nonatomic ,strong)NSMutableArray *dataArray;

@property (nonatomic ,strong)NSMutableDictionary *actionDictionary;

@property (nonatomic ,strong)UIButton *cancelBtn;

@end

@implementation CustomActionSheetAlertView
- (instancetype)init
{
   self = [super init];
   if (self) {
      self.hidden = YES;
      [self setBackgroundColor:[rgba(0, 0, 0, 0.60) colorWithAlphaComponent:0]];
   }
   return self;
}
- (instancetype)initWithFrame:(CGRect)frame
{
   self = [super initWithFrame:frame];
   if (self) {
      self.hidden = YES;
      [self setBackgroundColor:[rgba(0, 0, 0, 0.60) colorWithAlphaComponent:0]];
      [self addSubview:self.actionTabview];
       
   }
   return self;
}

#pragma mark UITableViewDelegate,UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 55;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.01;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
   return 0.01;
}
//- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
//{
//    UIView *view =[[UIView alloc] initWithFrame:CGRectMake(0, 0, LTScreenWidth,  62 + LTBottomDangerousAreaHeight + 13)];
//    view.backgroundColor = LTWhiteColor;
//   return view;
//}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellIdentifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
   [cell.contentView removeAllSubviews];
   
   NSDictionary *dic = [self.dataArray objectAtIndex:indexPath.row];
   NSString *title = dic[@"title"];
   UILabel *titlelab = ({
      UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 0, 17)];
      label.font = RFontWithSize(17);
      label.textColor = rgba(0, 0, 0, 1);
      label.numberOfLines = 1;
      label.textAlignment = NSTextAlignmentCenter;
      label;
   });
   if(dic[@"titleColor"])
   {
      titlelab.textColor = [UIColor colorWithHexString:dic[@"titleColor"]];
   }
    if(dic[@"font"])
    {
       titlelab.font = RFontWithSize([dic[@"font"] intValue]);
    }
   [titlelab setText:title];
   [titlelab sizeToFit];
   [titlelab setCenter:CGPointMake(self.actionTabview.width/2.0, 55/2.0)];
   [cell addSubview:titlelab];
   
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
   [self dissmisView];
   if ([self.actionDictionary objectForKey:self.dataArray[indexPath.row]]) {
      actionBlock block = [self.actionDictionary objectForKey:self.dataArray[indexPath.row]];
      block();
   }
  
}
#pragma -mark ---------- Private Method ----------
- (void)addActionWithTitle:(NSDictionary *)title Action:(actionBlock)action
{
   if (title) {
      [self.dataArray addObject:title];
   }
   if (action) {
      [self.actionDictionary setObject:action forKey:title];
   }
   [self.actionTabview reloadData];
   

}
- (void)showInView:(UIView *)InView
{
   [InView addSubview:self];
   [self setFrame:InView.bounds];
   [self addSubview:self.actionTabview];
   if(self.cancelTitleColor.length > 0)
   {
      [self.cancelBtn setTitleColor:[UIColor colorWithHexString:self.cancelTitleColor] forState:UIControlStateNormal];
   }
   [self.actionTabview reloadData];
   [self showView];
}
#pragma mark - event
- (void)dissmisView
{
   [UIView animateWithDuration:0.2 animations:^{
      [self setBackgroundColor:[rgba(0, 0, 0, 1) colorWithAlphaComponent:0.0]];
      self.actionTabview.y = LTScreenHeight;
      self.cancelBtn.y = LTScreenHeight;
   } completion:^(BOOL finished) {
      [self removeFromSuperview];
   }];
}
- (void)showView
{
   self.hidden = NO;
   self.actionTabview.height = 55*3 + 62 + LTBottomDangerousAreaHeight + 13;
   self.cancelBtn.y = self.actionTabview.y + self.actionTabview.height + 10;
   [UIView animateWithDuration:0.25 animations:^{
      [self setBackgroundColor:rgba(0, 0, 0, 0.6)];
      self.actionTabview.y = LTScreenHeight - self.actionTabview.height + 13;
      self.cancelBtn.y = LTScreenHeight - 55 - LTBottomDangerousAreaHeight;
   }];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
   UITouch *touch = touches.anyObject;
   CGPoint p = [touch locationInView:self];
   if (p.y < self.actionTabview.y) {
      [self dissmisView];
   }
}
#pragma -mark ---------- set ----------
- (UITableView *)actionTabview
{
   if (!_actionTabview)
   {
      _actionTabview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, LTScreenWidth, LTScreenHeight - LTBottomDangerousAreaHeight) style:UITableViewStylePlain];
      _actionTabview.frame = CGRectMake(0, LTScreenHeight, LTScreenWidth, 0);
      _actionTabview.backgroundColor = LTWhiteColor;
       [_actionTabview setDelegate:self];
       [_actionTabview setDataSource:self];
       [_actionTabview setSeparatorInset:UIEdgeInsetsZero];
      _actionTabview.separatorColor = rgba(242, 242, 242, 1);
      _actionTabview.tableFooterView = nil;
      _actionTabview.showsHorizontalScrollIndicator = NO;
      _actionTabview.showsVerticalScrollIndicator = NO;
      _actionTabview.bounces = NO;
       [_actionTabview setAllCornersWithCornerRadius:12];
   }
   return _actionTabview;
}
- (UIButton *)cancelBtn
{
   if (!_cancelBtn) {
      _cancelBtn =  ({
         
         UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
         button.titleLabel.font = RFontWithSize(17);
         [button setTitle:@"取消" forState:UIControlStateNormal];
         [button setTitleColor:rgba(0, 0, 0, 1) forState:UIControlStateNormal];
         [button addTarget:self action:@selector(dissmisView) forControlEvents:UIControlEventTouchUpInside];
         button.adjustsImageWhenHighlighted = NO;
         button;
         
      });
       _cancelBtn.clipsToBounds = NO;
       [_cancelBtn addSubview:separateLineWithColor(CGRectMake(0, -7, LTScreenWidth, 7), rgba(242, 242, 242, 1))];
      [_cancelBtn setBackgroundColor:LTWhiteColor];
      [_cancelBtn setFrame:CGRectMake(0, 0, LTScreenWidth, 55)];
      [self addSubview:_cancelBtn];
   }
   return _cancelBtn;
   
}
- (NSMutableArray *)dataArray
{
   if (!_dataArray) {
      _dataArray = [NSMutableArray arrayWithCapacity:0];
   }
   return _dataArray;
}
- (NSMutableDictionary *)actionDictionary
{
   if (!_actionDictionary) {
      _actionDictionary = [NSMutableDictionary dictionaryWithCapacity:0];
   }
   return _actionDictionary;
}
@end
